proc connect {} {
	global conns
	global n_conns
	global mes
	global CONFIG
	
	set use_ssl 0
	
	toplevel .connectdlg
	wm title .connectdlg [tr "Connect to..."]
	grid columnconfigure .connectdlg 0 -weight 1
	grid rowconfigure .connectdlg 0 -weight 1
	
	grid [ttk::frame .connectdlg.fr] -row 0 -column 0 -sticky nsew
	grid [ttk::label .connectdlg.fr.server_l -text [tr "Server"]]	-row 0 -column 0 -sticky w
	grid [ttk::entry .connectdlg.fr.server]				-row 0 -column 1 -sticky ew
	grid [ttk::label .connectdlg.fr.port_l -text [tr "Port"]]	-row 1 -column 0 -sticky w
	grid [ttk::entry .connectdlg.fr.port]				-row 1 -column 1 -sticky ew
	grid [ttk::label .connectdlg.fr.nick_l -text [tr "Nickname"]]	-row 2 -column 0 -sticky w
	grid [ttk::entry .connectdlg.fr.nick]				-row 2 -column 1 -sticky ew
	grid [ttk::label .connectdlg.fr.user_l -text [tr "Ident"]]	-row 3 -column 0 -sticky w
	grid [ttk::entry .connectdlg.fr.user]				-row 3 -column 1 -sticky ew
	grid [ttk::label .connectdlg.fr.name_l -text [tr "Real name"]]	-row 4 -column 0 -sticky w
	grid [ttk::entry .connectdlg.fr.name]				-row 4 -column 1 -sticky ew
	grid [ttk::checkbutton .connectdlg.fr.ssl -variable use_ssl -text [tr "Use SSL"]] -row 5 -column 0 
	grid [ttk::button .connectdlg.fr.btn -command "set done 1" -text [tr "Connect"]] \
		-row 5 -column 1 -sticky nse
	
	.connectdlg.fr.nick insert end $CONFIG(nick)
	.connectdlg.fr.user insert end $CONFIG(ident)
	.connectdlg.fr.name insert end $CONFIG(realname)
	.connectdlg.fr.port insert end 6667
	
    vwait done
    
	foreach var {server port nick user name} {
		set $var [.connectdlg.fr.$var get]
		if { "[set $var]" == "" } {
			tk_dialog .error [tr "Error"] [tr "You must fill all fields to connect!"] error 0 [tr "Dismiss"]
			destroy .connectdlg
			return 1
		}
	}
	
	set CONFIG(nick) [.connectdlg.fr.nick get]
	set CONFIG(ident) [.connectdlg.fr.user get]
	set CONFIG(realname) [.connectdlg.fr.name get]
	saveconfig
	
	if $use_ssl {
		set chid [set conns($server) [tls::socket $server $port]]
		tls::handshake $chid
	} else  {
		set chid [set conns($server) [socket $server $port]]
	}
    
	createnetwork $chid $server
	set parent $chid
	
	set str [tr "Logging in..."]
	$n_conns.$parent.console.mainbox.output insert end "$str\n" italic
	unset str
	
	puts $conns($server) "NICK $nick"
	puts $conns($server) "USER $user * * :$name"
	flush $conns($server)
	set mes($conns($server)) $nick
	
	fileevent $conns($server) readable "recv $conns($server) $parent"
	destroy .connectdlg
}

proc raw {stream msg} {
	puts "-> $stream: $msg"
	puts $stream "$msg\n"
	flush $stream
}
